#include "ui/models/device_model.h"
#include "device/device_info.h"

#include "modbus.h"

// QT
#include <QDebug>
//=========================================================================================
DeviceModel::DeviceModel(QVector<DeviceInfo*> *list, QObject *parent) : QAbstractItemModel(parent), oDeviceList(list)
{

}

DeviceModel::~DeviceModel()
{

}
//=========================================================================================
int DeviceModel::columnCount(const QModelIndex &parent) const
{
	return 1;
}

int DeviceModel::rowCount(const QModelIndex &parent) const
{
	if (!parent.isValid())
		return oDeviceList->count();

	return 0;
}
//=========================================================================================
QModelIndex DeviceModel::index(int row, int column, const QModelIndex &parent) const
{
	if (column < 0 || column >= columnCount(parent) || row < 0 ||
			parent.column() > 0 || row >= rowCount(parent))
		return QModelIndex();
	return createIndex(row, column, oDeviceList->at(row));
}

QModelIndex DeviceModel::parent(const QModelIndex &index) const
{
	return QModelIndex();
}

bool DeviceModel::hasChildren(const QModelIndex &parent) const
{
	return (rowCount(parent) > 0);
}
//=========================================================================================
Qt::ItemFlags DeviceModel::flags(const QModelIndex &index) const
{
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
//=========================================================================================
QVariant DeviceModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid())
		return QVariant();

	DeviceInfo *device = deviceOf(index);

	QString type;
	QString format;

	switch (index.column())
	{
	case 0:
		if (role != Qt::DisplayRole)
			return QVariant();
		switch (device->type())
		{
		case DT_DPE11:
			type = trUtf8("УЗД-11 ");
			break;
		case DT_UNKOWN:
			type = QString::fromUtf8("Неизвестное устройство ");
			break;
		}

		switch (device->format())
		{
		case ModbusPort::_8E1:
			format = "8E1";
			break;
		case ModbusPort::_8O1:
			format = "8O1";
			break;
		case ModbusPort::_8N15:
			format = "8N15";
			break;
		case ModbusPort::_8N2:
			format = "8N2";
			break;
		}

		/* ui.lstDevs->addItem(QObject::trUtf8("УЗД-11 ") + QString::number(dev.currentMin / (float)dev.currentScale) + "-" +
		 * QString::number(dev.currentMax / (float)dev.currentScale) + " v" + QString::number(dev.ver / 100.0, 'g', 3) + " / " +
		 * QString::number(dev.addr) + " / " + QString::number(dev.speed) + " / " + format + "/ " + dev.port + (old?QString::fromUtf8(" *Старый поиск"):""));
		*/
		if (device->type() != DT_UNKOWN)
		{
			int dec = (device->curScale() == 1 ? 0 : 1);
			type += QString::number(device->curMin(), 'F', dec) + "-" + QString::number(device->curMax(), 'F', dec) +
					" v" + QString::number(device->version() * 0.01, 'g', 3);
		}
		return type + "/" + QString::number(device->addr()) + "/" +
				QString::number(device->speed()) + "/" + format + "/COM" + QString::number(device->port());
	}
	return QVariant();
}

QVariant DeviceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (orientation == Qt::Vertical || role != Qt::DisplayRole)
		return QVariant();
	switch (section)
	{
	case 0:	return QString::fromUtf8("Устройство");
	}
	return QVariant();
}
//=========================================================================================
DeviceInfo *DeviceModel::deviceOf(const QModelIndex &index) const
{
	if (index.isValid())
		return static_cast<DeviceInfo*>(index.internalPointer());
	return 0;
}
//=========================================================================================
void DeviceModel::addItem()
{
	int count = rowCount();
	beginInsertRows(QModelIndex(), count, count);
	endInsertRows();
}
